<template>
	<view class="wrap">
		<view class="logo">学校公告系统</view>
		<view class="list">
				<view class="list-call">
					<view class="iconfont icon-avatar" style="font-size: 22px;color:#5473e8;"></view>
					<input class="u-input" type="text" v-model="username" maxlength="32" :placeholder="$t('login.placeholderAccount')" value="admin"/>
				</view>
				<view class="list-call">
					<view class="iconfont icon-key" style="font-size: 22px;color:#5473e8;"></view>
					<input class="u-input" type="text" v-model="password" maxlength="32" :placeholder="$t('login.placeholderPassword')" :password="!showPassword" value="admin123" />
					<image class="u-icon-right" :src="'/static/aidex/login/eye_' + (showPassword ? 'open' : 'close') + '.png'" @click="showPass()"></image>
				</view>
				<view class="list-call">
					<view class="iconfont icon-avatar" style="font-size: 22px;color:#5473e8;"></view>
					<uni-data-select v-model="schoolId" :localdata="schoolItems" placeholder="请选择学校"></uni-data-select>
				</view>
				<div style="padding:15rpx 0 0;">
					<view class="register">
						<navigator class="register-link" url="forget" open-type="navigate">{{$t('login.forget')}}</navigator>
					</view>
					<u-checkbox v-model="remember" active-color="#5473e8">{{$t('login.autoLogin')}}</u-checkbox>
				</div>
			</view>
			<view class="button"  @click="submit('1')"><text>登录</text></view>
			<view class="button"  @click="submit('10')"><text>单点登录</text></view>
		
		</view>
	</view>
</template>
<script>

/**
 * Copyright (c) 2013-Now http://aidex.vip All rights reserved.
 */

import base64 from '@/common/base64.js';
import { uniDataSelect } from '@dcloudio/uni-ui';

export default {
	components: {
	    uniDataSelect
	},
	data() {
		return {
			phoneNo:'',
			username: 'admin',
			password: '123456',
			loginType: 'currentPhone', 
			showPassword: false,
			remember: true,
			isValidCodeLogin: false,
			validCode: '',
			imgValidCodeSrc: null,
			list: [{name: '用户名'}],
			current: 0,
			activeColor: '#007aff',
			schoolId: '',
			schoolItems: [],
		};
	},
	onLaunch:function(){
	},
	onLoad(option) {
		// this.$u.toast(window.location.href);
		const token = this.getUrlParams('token')
		if(token){
			uni.setStorageSync('token', token);
			this.$u.api.getProfile().then(res => {
				if(res.code == 200){
					uni.setStorageSync("username",res.data.user.userName)
				}
			})
			setTimeout(() => {
				uni.reLaunch({
					url: '/pages/sys/project/index'
				});
			});
			// let tempToken = uni.getStorageSync('token')
			this.$u.toast(tempToken);
		}else{
			//this.towxLogin()
		}
	},
	created() {
	    this.getSchool();
	},
	methods: {
		getSchool(){
			this.$u.api.getSchools().then(res=>{
				res.data.forEach((item) => {
				  const school = {text : item.schoolname, value:  item.schoolid};
				  this.schoolItems.push(school);
				})
			})
		},
		showPass() {
			this.showPassword = !this.showPassword;
		},
		qiehuanLogin(){
			this.loginType = 'other'
		},
		onClickItem(index) {
							this.current = index;
					},
		refreshImgValidCode(e) {
			if (this.vuex_token == '') {
				this.$u.api.index().then(res => {
					this.imgValidCodeSrc = this.vuex_config.baseUrl + '/validCode?__sid='
						+ res.sessionid + '&t=' + new Date().getTime();
				});
			} else {
				this.imgValidCodeSrc = this.vuex_config.baseUrl + '/validCode?__sid='
						+ this.vuex_token + '&t=' + new Date().getTime();
			}
			this.validCode = '';
		},
		nextStep(){
			//验证码登录下一步
			uni.showLoading({
								title: '正在获取验证码',
								mask: true
							})
			this.$u.api.sendCode({
				phoneNo: this.phoneNo,
				validCodeType:'2'
			})
			.then(res => {
				if (res.code == '200') {
					setTimeout(() => {
						uni.navigateTo({
							url: '/pages/sys/login/code?phoneNo='+this.phoneNo
						});
					}, 500);
				}else{
					this.$u.toast(res.msg);
					setTimeout(() => {
						uni.navigateTo({
							url: '/pages/sys/login/code?phoneNo='+this.phoneNo
						});
					}, 500);
				}
			});
		},
		towxLogin(){
			const appid = "wwe7858c0d51254286";
			const agentid = "1000013";
			const uri = "http://emswx.kennede.com:49865/prod-api/unauth/authorize"
			this.toWxAuthorize(encodeURI(uri),appid,agentid);
		},
		toWxAuthorize(uri,appid,agentid){
			let url = 'https://open.weixin.qq.com/connect/oauth2/authorize?'+
			'appid='+appid+
			'&redirect_uri='+uri+
			'&response_type=code&scope=snsapi_userinfo&state=STATE'+
			'&agentid='+agentid+'#wechat_redirect';
			// this.$u.get(url).then(res=>{
			// 	this.$u.toast(res);
			// 	console.log(res)
			// })
			window.location.href = url
		},
		getUrlParams(key) {
		    // 通过 ? 分割获取后面的参数字符串
			if(window.location.href.indexOf('?')>0){
				let urlStr = window.location.href.split('?')[1]
				
				// 再通过 & 将每一个参数单独分割出来
				let paramsArr = urlStr.split('&')
				for(let i = 0;i < paramsArr.length;i++){
				    // 再通过 = 将每一个参数分割为 key:value 的形式
					let arr = paramsArr[i].split('=')
					if(key == arr[0] ){
						return arr[1];
					}
				}
			}
			return undefined;
		    
		},
		submit(loginType) {
			if(loginType == '10'){
				this.towxLogin()
				return;
			}
			if (this.username.length == 0) {
				this.$u.toast('请输入账号');
				return;
			}
			if (this.password.length == 0) {
				this.$u.toast('请输入密码');
				return;
			}
			if (this.schoolId.length == 0) {
				this.$u.toast('请选择学校');
				return;
			}
			this.$u.api.login({
				username: this.username,
				password: this.password,
				validCode: this.validCode,
				loginType: loginType,
				schoolid: this.schoolId
			})
			.then(res => {
				if(res.msg){
					this.$u.toast(res.msg);
				}
				if(200 == res.code){
					if (res.code == '200') {
						uni.setStorageSync('token', res.data.token);
						setTimeout(() => {
							uni.reLaunch({
								url: '/pages/sys/workbench/index'
							});
						}, 500);
					}
					if (res.isValidCodeLogin){
						this.isValidCodeLogin = true;
						this.refreshImgValidCode();
					}
				}
			
				
			});
		},
		wxLogin(res) {
			this.$u.toast('微信登录');
		},
		qqLogin() {
			this.$u.toast('QQ 登录');
		}
	}
};
</script>
<style lang="scss">
@import 'index.scss';

.logo {
	width: 80%;
	font-size: 58rpx;
	color: #5473e8;
	margin: 80rpx auto 80rpx auto;
	text-align: center;
}
.list-call-icon{
	color: #ff0000;
}
.currentPhone-box{
	text-align: center;
	padding: 40rpx 80rpx;
	.number-text{
		color: #000000;
		font-size: 60rpx;
	}
	.other-text{
		color: #999999;
		font-size: 26rpx;
		padding: 20rpx 0;
	}
	.u-btn{
		margin: 30rpx auto;
	}
	.u-hairline-border{
	    border: 1px solid #fff;
	}
}

.register {
	display:inline-block;
	color: #5473e8;
	height: 40rpx;
	line-height: 40rpx;
	font-size: 28rpx;
	float:right;
	margin-top: 6rpx;
}
.register-link{
	float: right;
	padding: 0 16rpx;
}
.reg-link{
	display: inline-block;
	color: #5473e8;
}
.oauth2 {
	display: flex;
	flex-direction: row;
	justify-content: space-around;
	margin: 0rpx 100rpx 30rpx;

	image {
		height: 80rpx;
		width: 80rpx;
	}
}
.u-tabs{
	padding: 0 70rpx;
}

</style>
